package prototype.data;

import com.google.gson.JsonObject;
import prototype.constant.ELanguage;
import prototype.constant.EMessageConst;
import prototype.constant.EObjectType;
import prototype.constant.data.DictionaryConst;
import prototype.utils.JsonUtils;

import javax.persistence.*;

/**
 * словарь
 */
@Entity
@Table(name = DictionaryConst.TABLE_NAME,
		uniqueConstraints = @UniqueConstraint(columnNames = {DictionaryConst.S_LANGUAGE, DictionaryConst.S_CODE}))
public class Dictionary extends AbstractObject implements DictionaryConst {
	/**
	 * язык
	 */
	private ELanguage language;
	/**
	 * код текста в словаре
	 */
	private EMessageConst code;
	/**
	 * текст
	 */
	private String text;

	{
		setObjectType(EObjectType.DICTIONARY);
	}

	public Dictionary() {
	}

	public Dictionary(Long id, ELanguage language, EMessageConst code, String text) {
		super(id);
		this.language = language;
		this.code = code;
		this.text = text;
	}

	@Column(name = S_LANGUAGE)
	@Enumerated(EnumType.STRING)
	public ELanguage getLanguage() {
		return language;
	}

	public void setLanguage(ELanguage language) {
		this.language = language;
	}

	public void setLanguage(String languageStr) {
		this.language = (languageStr != null) ? ELanguage.valueOf(languageStr) : null;
	}

	@Column(name = S_CODE)
	@Enumerated(EnumType.STRING)
	public EMessageConst getCode() {
		return code;
	}

	public void setCode(EMessageConst code) {
		this.code = code;
	}

	public void setCode(String code) {
		this.code = (code != null) ? EMessageConst.valueOf(code) : null;
	}

	@Column(name = S_TEXT)
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	// --------------------------- JSON ---------------------------------------

	public Dictionary(final JsonObject jsonObject) {
		super(jsonObject);
		setLanguage(JsonUtils.getString(jsonObject, S_LANGUAGE));
		setCode(JsonUtils.getString(jsonObject, S_CODE));
		this.text = JsonUtils.getString(jsonObject, S_TEXT);
	}

	@Override
	public JsonObject toJsonObject() {
		final JsonObject jsonObject = super.toJsonObject();
		JsonUtils.set(jsonObject, S_LANGUAGE, language.name());
		JsonUtils.set(jsonObject, S_CODE, code.name());
		JsonUtils.set(jsonObject, S_TEXT, text);
		return jsonObject;
	}
}
